package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ScanType;
import ywpt.service.config.ScanTypeService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ScanTypeAction extends BaseAction implements
ModelDriven<ScanType>, Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8534616393108607762L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private ScanType scanType;
	private List<ScanType> scanTypeList = new ArrayList<ScanType>();
	private ScanTypeService scanTypeService;
	 
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ScanType getScanType() {
		return scanType;
	}

	public void setScanType(ScanType scanType) {
		this.scanType = scanType;
	}

	public List<ScanType> getScanTypeList() {
		return scanTypeList;
	}

	public void setScanTypeList(List<ScanType> scanTypeList) {
		this.scanTypeList = scanTypeList;
	}
 

	public ScanTypeService getScanTypeService() {
		return scanTypeService;
	}

	public void setScanTypeService(ScanTypeService scanTypeService) {
		this.scanTypeService = scanTypeService;
	}

	@Override
	public ScanType getModel() {
		return this.scanType;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.scanType = this.scanTypeService.getScanTypeById(id);
		}else{
			this.scanType = new ScanType();
		}
	}

	// **************************数据交互方法*******************************

	@SuppressWarnings("unchecked")
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(ScanType.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = scanTypeService.getScanTypeCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		scanTypeList.addAll(scanTypeService.getScanTypes(query, pageSize, rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		String code = this.scanTypeService.createScanTypeCode();
		this.scanType.setCode(code);
		return INPUT;
	}
	
	public String edit() 
	{
		this.scanType=this.scanTypeService.getScanTypeById(id);
		return INPUT;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				scanTypeService.saveScanType(scanType);
			} 
			else 
			{
				scanTypeService.updatScanType(scanType);
			}
			this.addActionMessage("信息已保存!");
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
//			DetachedCriteria contractQuery = DetachedCriteria.forClass(ContractInfo.class);
//			contractQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows1 = contractService.getContractInfoCountByCriteria(contractQuery);			
//
//			DetachedCriteria jobQuery = DetachedCriteria.forClass(JobInfo.class);
//			jobQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows2 = jobService.getJobInfoCountByCriteria(jobQuery);
//			
//			DetachedCriteria deviceQuery = DetachedCriteria.forClass(DeviceInfo.class);
//			deviceQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows3 = deviceService.getDeviceInfoCountByCriteria(deviceQuery);
			
//			if(totalRows1>0 || totalRows2>0 || totalRows3>0)
//			{
//				this.addActionMessage("该客户已经发生过业务逻辑，不能删除！");
//				return INPUT;
//			}
			scanTypeService.removeScanType(id);
			return SUCCESS;
		}
		this.addActionMessage("编号为空，请选择具体的记录！");
		return INPUT;
	}	
}
